﻿using Huirui.Cavan.Application.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Huirui.Cavan.TestAppService
{
    public class TestStockInAppService
    {
        private IApplicationContext context;
        private IDepartmentAppService DepartmentAppService;
        private IEmployeeAppService EmployeeAppService;
        private IAccountAppService AccountAppService;
        private IStockInOutTypeAppService StockInOutTypeAppService;
        private ISupplierAppService SupplierAppService;
        private IStockInOutAppService StockInOutAppService;
        private IWareHouseAppService WareHouseAppService;
        private IStockAppService StockAppService;
        private IPurchaseOrderAppService PurchaseOrderAppService;

        [SetUp]
        public void SetUp()
        {
            context = ContextRegistry.GetContext();
            HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
        }

        private void GetAppService()
        {
            this.DepartmentAppService = context.GetObject(SystemModuleConst.DEPARTMENTAPPSERVICE) as IDepartmentAppService;
            this.EmployeeAppService = context.GetObject(SystemModuleConst.EMPLOYEEAPPSERVICE) as IEmployeeAppService;
            this.AccountAppService = context.GetObject(SystemModuleConst.ACCOUNTAPPSERVICE) as IAccountAppService;
            this.StockInOutTypeAppService = context.GetObject(StockModuleConst.STOCKINOUTTYPEAPPSERVICE) as IStockInOutTypeAppService;
            this.SupplierAppService = context.GetObject(SystemModuleConst.SUPPLIERAPPSERVICE) as ISupplierAppService;
            this.StockInOutAppService = context.GetObject(StockModuleConst.STOCKINOUTAPPSERVICE) as IStockInOutAppService;
            this.WareHouseAppService = context.GetObject(StockModuleConst.WAREHOUSEAPPSERVICE) as IWareHouseAppService;
            this.StockAppService = context.GetObject(StockModuleConst.STOCKAPPSERVICE) as IStockAppService;
        }

        [Test]
        public void TestAddOrUpdate()
        {
            GetAppService();
            Department department = this.DepartmentAppService.GetById("001");
            Employee employee = this.EmployeeAppService.GetById("001_e11");
            Account account = this.AccountAppService.GetById("001");
            Supplier supplier = this.SupplierAppService.GetById("001_gys001");
            StockInOutType type = this.StockInOutTypeAppService.GetById(1);
            WareHouse wareHouse = this.WareHouseAppService.GetById("001_wh_001");
            Stock stock = this.StockAppService.GetById("001_ch001");

            StockInOutDetail stockInDetail1 = new StockInOutDetail()
            {
                Cost = 100,
                CostType = StockIdentifier.CostType.StandardPrice,
                Description = string.Empty,
                FromAccount = account,
                Number = 10,
                Stock = stock,
                UnitCost = 10,
                WareHouse = wareHouse,
                TaxRate = (decimal)0.15
            };
            IList<StockInOutDetail> details = new List<StockInOutDetail>();
            details.Add(stockInDetail1);

            StockInOut stockIn = new StockInOut()
            {
                Department = department,
                Employee = employee,
                FromAccount = account,
                InOutDate = DateTime.Now,
                ProductionNumber = string.Empty,
                Project = null,
                Remark = "test",
                Supplier = supplier,
                Type = type,
                StockInOutDetails = details,
                Contact = "001_e11",
                ContactTelephone = "1234234",
                Currency = "RMB",
                CurrencyRate = (decimal)1.0
            };

            BoolGenericResult<StockInOut> result = this.StockInOutAppService.AddOrUpdate(stockIn);

            Assert.IsNotNull(result);
        }

        [Test]
        public void TestAddStockInFromPurchaseOrder()
        {
            GetAppService();
            Account account = this.AccountAppService.GetById("001");
            WareHouse wareHouse = this.WareHouseAppService.GetById("001_wh_001");
            Stock stock = this.StockAppService.GetById("001_ch001");

            StockInOutDetail stockInDetail1 = new StockInOutDetail()
            {
                Cost = 50,
                CostType = StockIdentifier.CostType.StandardPrice,
                Description = string.Empty,
                FromAccount = account,
                Number = 5,
                Stock = stock,
                UnitCost = 10,
                WareHouse = wareHouse
            };
            IList<StockInOutDetail> details = new List<StockInOutDetail>();
            details.Add(stockInDetail1);

            AddStockInByPurchaseOrderResult result = null;

            Assert.IsNotEmpty(result.AddStockInResult.Object.Id);
        }
    }
}
